package com.one.common.utils;

import android.text.TextUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FormatUtils {
	public static final boolean ERROR_TO_BOOLEAN = false;
	public static final int ERROR_TO_INT = -100;
	public static final long ERROR_TO_LONG = -100L;
	public static final float ERROR_TO_FLOAT = -100F;
	public static final double ERROR_TO_DOUBLE = -100D;

	/**
	 * @param data
	 * @return
	 */
	public static boolean strToBoolean(String data) {
		if ("1".equals(data)) {
			return true;
		}
		if ("0".equals(data)) {
			return false;
		}
		try {
			return Boolean.parseBoolean(data);
		} catch (Exception e) {
			return ERROR_TO_BOOLEAN;
		}
	}

	/**
	 * @param data
	 * @return
	 */
	public static int strToInt(String data, int errorValue) {
		try {
			return Integer.parseInt(data);
		} catch (Exception e) {
			return errorValue;
		}
	}

	/**
	 * @param data
	 * @return
	 */
	public static int strToInt(String data) {
		return strToInt(data, ERROR_TO_INT);
	}

	/**
	 * @param data
	 * @return
	 */
	public static long strToLong(String data, long errorValue) {
		try {
			return Long.parseLong(data);
		} catch (Exception e) {
			return errorValue;
		}
	}

	/**
	 * @param data
	 * @return
	 */
	public static long strToLong(String data) {
		return strToLong(data, ERROR_TO_LONG);
	}

	/**
	 * @param data
	 * @return
	 */
	public static float strToFloat(String data, float errorValue) {
		try {
			return Float.parseFloat(data);
		} catch (Exception e) {
			return errorValue;
		}
	}

	/**
	 * @param data
	 * @return
	 */
	public static float strToFloat(String data) {
		return strToFloat(data, ERROR_TO_FLOAT);
	}

	/**
	 * @param data
	 * @return
	 */
	public static double strToDouble(String data, double errorValue) {
		try {
			return Double.parseDouble(data);
		} catch (Exception e) {
			return errorValue;
		}
	}

	/**
	 * @param data
	 * @return
	 */
	public static double strToDouble(String data) {
		return strToDouble(data, ERROR_TO_DOUBLE);
	}

	/**
	 * @param data
	 * @return
	 */
	public static String strToDoubleStr(String data) {
		try {
			Double doubleData = Double.parseDouble(data);
			return removeDecimalZero(doubleData + "");
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 将Double至保留一位小数
	 *
	 * @param data
	 * @return
	 */
	public static String formatDoubleToStrWithSingleDecimal(String data) {
		try {
			Double doubleData = Double.parseDouble(data);
			DecimalFormat df = new DecimalFormat("0.0");
			return df.format(doubleData);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 将Double至保留二位小数
	 *
	 * @param data
	 * @return
	 */
	public static String formatDoubleToStrWithTwoDecimal(String data) {
		try {
			Double doubleData = Double.parseDouble(data);
			DecimalFormat df = new DecimalFormat("0.00");
			return df.format(doubleData);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 去掉小数点后面的0
	 *
	 * @param s
	 * @return
	 */
	public static String removeDecimalZero(String s) {
		if (s.indexOf(".") > 0) {
			s = s.replaceAll("0+?$", "");
			s = s.replaceAll("[.]$", "");
		}
		return s;
	}

    /**
     * 向上取整并去除0
     *
     * @param s
     * @return
     */
    public static String removeDecimal(String s) {
        return removeDecimalZero(Math.ceil(Double.parseDouble(s))+"");
    }

	/**
	 * BigDecimal 转成 String(四舍五入)
	 *
	 * @return
	 */
	public static final String convertBigDecimalToString(String doString, int Scale) {

		if (TextUtils.isEmpty(doString)) {
			return "";
		}
		BigDecimal bigDecimal = new BigDecimal(doString);
		BigDecimal b = bigDecimal.setScale(Scale, BigDecimal.ROUND_HALF_UP);

		return b.toString();
	}

	/**
	 * String(BigDecimal) 转成 String(向上取整)
	 *
	 * @return
	 */
	public static final String convertBigDecimalToStringRoundup(String doString, int Scale) {
		if (TextUtils.isEmpty(doString)) {
			return "";
		}
		BigDecimal bigDecimal = new BigDecimal(doString);

		BigDecimal b = bigDecimal.setScale(Scale, BigDecimal.ROUND_UP);

		return b.toString();
	}

	/**
	 * 距离米转换为公里
	 *
	 * @param distance
	 * @return
	 */
	public static String changeToStr(String distance) {
		String str = "0";
		if (!TextUtils.isEmpty(distance)) {
			float d = strToFloat(distance);
			float ret = 0.1f;
			if (d > 100) {
				ret = (d / 1000);
			}

			str = formatDoubleToStrWithSingleDecimal("" + ret);
		}
		return str;
	}

	public static String getStrWithComma(String... strArray) {
		String result = "";
		String[] str;
		str = strArray;
		for (int i = 0; i < str.length; i++) {
			result = str[i] + ",";
		}
		return result.substring(0, result.length() - 1);
	}





	public static final String convertBigDecimalToString(BigDecimal bigDecimal,
														 int Scale) {

		if (bigDecimal == null) {
			return "";
		}
		BigDecimal b = bigDecimal.setScale(Scale, BigDecimal.ROUND_HALF_UP);
		String bigDecimalStr  = b.toString();
		// Scale==2  重量体积去0
		if(Scale==2){
			if(".00".equals(bigDecimalStr.substring(bigDecimalStr.length()-3,bigDecimalStr.length()))){
				bigDecimalStr = bigDecimalStr.substring(0,bigDecimalStr.length()-3);
			}else if("0".equals(bigDecimalStr.substring(bigDecimalStr.length()-1,bigDecimalStr.length()))){
				bigDecimalStr = bigDecimalStr.substring(0,bigDecimalStr.length()-1);
			}else if(".0".equals(bigDecimalStr.substring(bigDecimalStr.length()-2,bigDecimalStr.length()))){
				bigDecimalStr = bigDecimalStr.substring(0,bigDecimalStr.length()-2);
			}
		}
		return bigDecimalStr;
	}

	// 判断一个字符串是否含有数字

	public static boolean hasDigit(String content) {
		boolean flag = false;
		Pattern p = Pattern.compile(".*\\d+.*");
		Matcher m = p.matcher(content);
		if (m.matches())
			flag = true;
		return flag;
	}

	public static int getIntAmount(String amount) {
		return (int) FormatUtils.strToDouble(amount, 0D);
	}
}
